<?php
class AnswerModify extends Model {
	public function delete($id) {
		$sql = "DELETE FROM `exercise_modify` WHERE `modify_id` = $id";
		return $this->del ( $sql );
	}
	
	public function getList($userId, $type, $timeStamp, $pageType, $pageSize) {
		if ($timeStamp == "null" || $timeStamp == "" || $timeStamp == "NULL") {
			$sql = "SELECT `exercise_modify`.`modify_id` AS `quesModifyId`, 
						`exercise_modify`.`ex_id` AS `quesId`, 
						`exercise`.`ex_title` AS `quesTitle`, 
						`exercise_modify`.`modify_create_time` AS `quesModifyTime`, 
						`exercise_modify`.`modify_text` AS `quesModifyAnswer`,
						`exercise_modify`.`modify_id` AS `timeStamp`
					FROM `exercise_modify` INNER JOIN `exercise` ON `exercise_modify`.`ex_id` = `exercise.ex_id`
					WHERE `modify_create_userid` = $userId AND `modify_type` = $type 
					ORDER BY `exercise_modify.modify_id` DESC LIMIT $pageSize";
		} else if ($pageType == "pre") {
			$sql = "SELECT `exercise_modify`.`modify_id` AS `quesModifyId`,
						`exercise_modify`.`ex_id` AS `quesId`,
						`exercise`.`ex_title` AS `quesTitle`,
						`exercise_modify`.`modify_create_time` AS `quesModifyTime`,
						`exercise_modify`.`modify_text` AS `quesModifyAnswer`,
						`exercise_modify`.`modify_id` AS `timeStamp`
					FROM `exercise_modify` INNER JOIN `exercise` ON `exercise_modify`.`ex_id` = `exercise.ex_id`
					WHERE `modify_create_userid` = $userId AND `exercise_modify`.`modify_id` > $timeStamp AND `modify_type` = $type 
					ORDER BY `exercise_modify.modify_id` DESC LIMIT $pageSize";
		} else {
			$sql = "SELECT `exercise_modify`.`modify_id` AS `quesModifyId`,
						`exercise_modify`.`ex_id` AS `quesId`,
						`exercise`.`ex_title` AS `quesTitle`,
						`exercise_modify`.`modify_create_time` AS `quesModifyTime`,
						`exercise_modify`.`modify_text` AS `quesModifyAnswer`,
						`exercise_modify`.`modify_id` AS `timeStamp`
					FROM `exercise_modify` INNER JOIN `exercise` ON `exercise_modify`.`ex_id` = `exercise.ex_id`
					WHERE `modify_create_userid` = $userId AND `exercise_modify`.`modify_id` < $timeStamp AND `modify_type` = $type 
					ORDER BY `exercise_modify.modify_id` DESC LIMIT $pageSize";
		}
		return $this->fetchAll($sql);
	}
	
	public function searchList($userId,  $type, $timeStamp, $pageType, $pageSize, $knowsId, $skillId) {
		$sql = "SELECT `exercise_modify`.`modify_id` AS `quesModifyId`,
					`exercise_modify`.`ex_id` AS `quesId`,
					`exercise`.`ex_title` AS `quesTitle`,
					`exercise_modify`.`modify_create_time` AS `quesModifyTime`,
					`exercise_modify`.`modify_text` AS `quesModifyAnswer`,
					`exercise_modify`.`modify_id` AS `timeStamp`
				FROM exercise_modify LEFT JOIN exercise ON exercise_modify.ex_id = exercise.ex_id 
					 LEFT JOIN exercise_knowledge ON exercise_modify.ex_id = exercise_knowledge.ex_id
					 LEFT JOIN exercise_skill ON exercise_modify.ex_id = exercise_skill.ex_id
				WHERE `modify_create_userid` = $userId AND `modify_type` = $type";
		if ($timeStamp == "null" || $timeStamp == "" || $timeStamp == "NULL") {
				
		} else if ($pageType == "pre") {
			$sql .= "`answer_id` > $timeStamp AND ";
		} else if ($pageType == "more") {
			$sql .= "`answer_id` < $timeStamp AND ";
		} else {
			return false;
		}
		if ($knowsId != "-1") {
			$knowsIds = split("_", $knowsId);
			$sql .= "(";
			for ($i = 0; $i < count($knowsIds) - 1 ; $i++) {
				$sql .= "`knows_id` = $knowsIds[$i] OR ";
			}
			$sql .= "`knows_id` = ".$knowsIds[count($knowsIds)-1].") AND ";
		}
		if ($skillId != "-1") {
			$skillIds = split("_", $skillId);
			$sql .= "(";
			for ($i = 0; $i < count($skillIds) - 1 ; $i++) {
				$sql .= "`knows_id` = $skillIds[$i] OR ";
			}
			$sql .= "`knows_id` = ".$skillIds[count($skillIds)-1].") AND ";
		}
		$sql .= "1=1 ORDER BY videoId DESC";
// 		echo $sql;
		return $this->fetchAll($sql);
	}
}

?>